package org.example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Hello world!
 */
public class AppLambdaStream {

   static List<Person> personList = Arrays.asList(
            new Person("张三", 33),
            new Person("李四", 24),
            new Person("王五", 25)
    );

    public static void main(String[] args) {
        show1();
        System.out.println("----------------分割线------------------");
        show2();
    }

    public static void show1() {
        List<String> list = Arrays.asList("aa", "bb", "cc");

        // 方式1,简单的map
        Stream<Stream<Character>> streamStream = list.stream()
                .map(e -> AppLambdaStream.filterCharacter(e));

        //拆解步骤
        Stream<String> strStream = list.stream();

//     <R> Stream<R> map(Function<T, R> mapper);
//                                                             "Stream<Character>" 返回值整体做为R
        Stream<Stream<Character>> streamStream1 = strStream.map( e -> AppLambdaStream.filterCharacter(e));

        streamStream1.forEach(sm -> {
            sm.forEach(character -> System.out.println(character));
        });

        System.out.println("-------------------------------------");
        // 方式2:flatMap, 直接拍平

        Stream<Character> characterStream = list.stream()
                .flatMap(s -> AppLambdaStream.filterCharacter(s));

        //拆解
        Stream<String> strStream2 = list.stream();
        //   <R> Stream<R> flatMap(Function< T, Stream<R> > mapper);
        //                                                                   Stream<"Character"> 其中的Character做为R
        Stream<Character> characterStream1 = strStream2.flatMap(s -> filterCharacter(s));
        characterStream1.forEach(System.out::println);

    }

    public static Stream<Character> filterCharacter(String str) {
        List<Character> list = new ArrayList<>();

        for (Character ch : str.toCharArray()
        ) {
            list.add(ch);
        }
        return list.stream();
    }

    public static void show2() {
        List<String> collect = personList.stream().
                map(e -> e.getName())
//        interface Stream<T>
//		<R, A> R collect(Collector<? super T, A, R> collector);
//		<R, A> R collect(Collector<T, A, R> collector);
//		<R, A> R collect(Collector<String, A, R> collector);
//
//        class Collectors
//        public static <T>  Collector<T, ?, List<T>> toList()
//        public static <T>  Collector<String, ?, List<String>> toList()

                .collect(Collectors.toList());

        System.out.println(collect);
    }

}
